package gui;

import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JTextArea;

import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.*;
import java.text.NumberFormat;
import java.text.ParseException;
import model.Model;
import dao.*;

public class MeetstationView extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel jPanel = null;
	private JPanel jPanel1 = null;
	private JLabel jLabel = null;
	private JButton updateButton = null;
	private JComboBox jComboBox = null;
	private JScrollPane jScrollPane = null;
	private JPanel jPanel2 = null;
	private JLabel jLabel1 = null;
	private JTextField idField = null;
	private JLabel jLabel2 = null;
	private JTextField plaatsField = null;
	private JLabel jLabel3 = null;
	private JTextField positieField = null;
	private JLabel jLabel4 = null;
	private JTextField terreinField = null;
	private JLabel jLabel5 = null;
	private JTextArea omgevingArea = null;
	private JLabel jLabel6 = null;
	private JTextField grondField = null;
	private JLabel jLabel7 = null;
	private JTextField baroField = null;
	private JLabel jLabel8 = null;
	private JTextField windField = null;
	private JButton createButton = null;

	private DefaultComboBoxModel boxModel;
	private ActionListener listener; // @jve:decl-index=0:
	private JButton deleteButton = null;
	private NumberFormat fmt = NumberFormat.getInstance();
	private Model model = null;

	/**
	 * This is the default constructor
	 */
	public MeetstationView(Model model) {
		super();
		this.model = model;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(462, 286);
		this.setLayout(new BorderLayout());
		this.add(getJPanel(), BorderLayout.NORTH);
		this.add(getJPanel1(), BorderLayout.SOUTH);
		this.add(getJScrollPane(), BorderLayout.CENTER);
		listener = new Luisteraar();
		boxModel = new DefaultComboBoxModel();
		boxModel.addElement("");

		ArrayList<String> stations;
		try {
			stations = (new MeetstationDAO()).getAlleStationNamen();
			for (int i = 0; i < stations.size(); i++) {
				boxModel.addElement(stations.get(i));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		jComboBox.setModel(boxModel);
		jComboBox.addActionListener(listener);
		updateButton.addActionListener(listener);
		createButton.addActionListener(listener);
		deleteButton.addActionListener(listener);
	}

	private class Luisteraar implements ActionListener {

		public void actionPerformed(ActionEvent e) {

			if (e.getSource().equals(jComboBox)) {
				JComboBox sender = (JComboBox) e.getSource();

				String item = (String) sender.getSelectedItem();
				String plaatsnaam = "";
				plaatsnaam = item;
				try {
					loadMetaData(plaatsnaam);
				} catch (SQLException e1) {
					idField.setText("");
					plaatsField.setText("");
					positieField.setText("");
					terreinField.setText("");
					omgevingArea.setText("");
					grondField.setText("");
					baroField.setText("");
					windField.setText("");
					e1.printStackTrace();
				}
			} else if (e.getSource().equals(updateButton)) {
				System.out.println("updateButton");
				int id = Integer.parseInt(idField.getText());
				String plaats = plaatsField.getText();
				String pos = positieField.getText();
				String omgeving = omgevingArea.getText();
				String grond = grondField.getText();
				double terrein = 0;
				double baro = 0;
				double wind = 0;

				try {
					terrein = fmt.parse(terreinField.getText()).doubleValue();
					baro = fmt.parse(baroField.getText()).doubleValue();
					wind = fmt.parse(windField.getText()).doubleValue();
				} catch (ParseException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}

				model.Meetstation meetstation = new model.Meetstation(id,
						plaats, pos, terrein, omgeving, grond, baro, wind);
				
				try 
				{
					(new MeetstationDAO()).updateMeetstation(meetstation);
				} 
				catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			} else if (e.getSource().equals(createButton)) {
				System.out.println("createButton");
				int id = Integer.parseInt(idField.getText());
				String plaats = plaatsField.getText();
				String pos = positieField.getText();
				String omgeving = omgevingArea.getText();
				String grond = grondField.getText();
				double terrein = 0;
				double baro = 0;
				double wind = 0;

				try {
					terrein = fmt.parse(terreinField.getText()).doubleValue();
					baro = fmt.parse(baroField.getText()).doubleValue();
					wind = fmt.parse(windField.getText()).doubleValue();
				} catch (ParseException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}

				model.Meetstation meetstation = new model.Meetstation(id,
						plaats, pos, terrein, omgeving, grond, baro, wind);
				
				try 
				{
					(new MeetstationDAO()).createMeetstation(meetstation);
				} 
				catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			} else if (e.getSource().equals(deleteButton)) {
				System.out.println("deleteButton");
				int id = Integer.parseInt(idField.getText().trim());

				try 
				{
					(new MeetstationDAO()).deleteMeetstation(id);
				} 
				catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
	}

	public void loadMetaData(String plaatsnaam) throws SQLException {
		model.Meetstation meetstation = (new MeetstationDAO())
				.getStationByName(plaatsnaam);
		idField.setText(""+meetstation.getStationID());
		plaatsField.setText(meetstation.getPlaats());
		positieField.setText(meetstation.getPositie());
		terreinField.setText("" + meetstation.getTerreinHoogte());
		omgevingArea.setText(meetstation.getOmgeving());
		grondField.setText(meetstation.getGrondsoort());
		baroField.setText("" + meetstation.getBaroHoogte());
		windField.setText("" + meetstation.getWindHoogte());
		model.setMeetstation(meetstation);
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints1.gridy = 1;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.gridx = 0;
			jLabel = new JLabel();
			jLabel.setText("Meetstations");
			jLabel.setFont(new Font("Dialog", Font.BOLD, 18));
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
			jPanel.add(jLabel, new GridBagConstraints());
			jPanel.add(getJComboBox(), gridBagConstraints1);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints18 = new GridBagConstraints();
			gridBagConstraints18.gridx = 3;
			gridBagConstraints18.gridy = 0;
			GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
			gridBagConstraints17.gridx = 2;
			gridBagConstraints17.gridy = 0;
			GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
			gridBagConstraints16.gridx = 1;
			gridBagConstraints16.gridy = 0;
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
			jPanel1.add(getUpdateButton(), gridBagConstraints17);
			jPanel1.add(getCreateButton(), gridBagConstraints16);
			jPanel1.add(getDeleteButton(), gridBagConstraints18);
		}
		return jPanel1;
	}

	/**
	 * This method initializes updateButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getUpdateButton() {
		if (updateButton == null) {
			updateButton = new JButton();
			updateButton.setText("UPDATE");
		}
		return updateButton;
	}

	/**
	 * This method initializes jComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.setPreferredSize(new Dimension(120, 24));
		}
		return jComboBox;
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane
					.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
			jScrollPane.setViewportView(getJPanel2());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jPanel2
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
			gridBagConstraints15.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints15.gridy = 7;
			gridBagConstraints15.weightx = 1.0;
			gridBagConstraints15.gridx = 1;
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			gridBagConstraints14.gridx = 0;
			gridBagConstraints14.gridy = 7;
			jLabel8 = new JLabel();
			jLabel8.setText("hoogte windmeter");
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints13.gridy = 6;
			gridBagConstraints13.weightx = 1.0;
			gridBagConstraints13.gridx = 1;
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 0;
			gridBagConstraints12.gridy = 6;
			jLabel7 = new JLabel();
			jLabel7.setText("hoogte barometer");
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints11.gridy = 5;
			gridBagConstraints11.weightx = 1.0;
			gridBagConstraints11.gridx = 1;
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.gridx = 0;
			gridBagConstraints10.gridy = 5;
			jLabel6 = new JLabel();
			jLabel6.setText("grondsoort");
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.fill = GridBagConstraints.BOTH;
			gridBagConstraints9.gridy = 4;
			gridBagConstraints9.weightx = 1.0;
			gridBagConstraints9.weighty = 1.0;
			gridBagConstraints9.gridx = 1;
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 0;
			gridBagConstraints8.gridy = 4;
			jLabel5 = new JLabel();
			jLabel5.setText("omgeving");
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints7.gridy = 3;
			gridBagConstraints7.weightx = 1.0;
			gridBagConstraints7.gridx = 1;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.gridy = 3;
			jLabel4 = new JLabel();
			jLabel4.setText("terrein hoogte");
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints5.gridy = 2;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.gridx = 1;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.gridy = 2;
			jLabel3 = new JLabel();
			jLabel3.setText("positie");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridy = 1;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.gridx = 1;
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 0;
			gridBagConstraints21.gridy = 1;
			jLabel2 = new JLabel();
			jLabel2.setText("plaats");
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.gridy = 0;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			jLabel1 = new JLabel();
			jLabel1.setText("station_id");
			jPanel2 = new JPanel();
			jPanel2.setLayout(new GridBagLayout());
			jPanel2.add(jLabel1, gridBagConstraints);
			jPanel2.add(getIdField(), gridBagConstraints2);
			jPanel2.add(jLabel2, gridBagConstraints21);
			jPanel2.add(getPlaatsField(), gridBagConstraints3);
			jPanel2.add(jLabel3, gridBagConstraints4);
			jPanel2.add(getPositieField(), gridBagConstraints5);
			jPanel2.add(jLabel4, gridBagConstraints6);
			jPanel2.add(getTerreinField(), gridBagConstraints7);
			jPanel2.add(jLabel5, gridBagConstraints8);
			jPanel2.add(getOmgevingArea(), gridBagConstraints9);
			jPanel2.add(jLabel6, gridBagConstraints10);
			jPanel2.add(getGrondField(), gridBagConstraints11);
			jPanel2.add(jLabel7, gridBagConstraints12);
			jPanel2.add(getBaroField(), gridBagConstraints13);
			jPanel2.add(jLabel8, gridBagConstraints14);
			jPanel2.add(getWindField(), gridBagConstraints15);
		}
		return jPanel2;
	}

	/**
	 * This method initializes idField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getIdField() {
		if (idField == null) {
			idField = new JTextField();
		}
		return idField;
	}

	/**
	 * This method initializes plaatsField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getPlaatsField() {
		if (plaatsField == null) {
			plaatsField = new JTextField();
		}
		return plaatsField;
	}

	/**
	 * This method initializes positieField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getPositieField() {
		if (positieField == null) {
			positieField = new JTextField();
			positieField.setColumns(0);
		}
		return positieField;
	}

	/**
	 * This method initializes terreinField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTerreinField() {
		if (terreinField == null) {
			terreinField = new JTextField();
		}
		return terreinField;
	}

	/**
	 * This method initializes omgevingArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getOmgevingArea() {
		if (omgevingArea == null) {
			omgevingArea = new JTextArea();
			omgevingArea.setLineWrap(true);
			omgevingArea.setRows(0);
		}
		return omgevingArea;
	}

	/**
	 * This method initializes grondField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getGrondField() {
		if (grondField == null) {
			grondField = new JTextField();
		}
		return grondField;
	}

	/**
	 * This method initializes baroField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getBaroField() {
		if (baroField == null) {
			baroField = new JTextField();
		}
		return baroField;
	}

	/**
	 * This method initializes windField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getWindField() {
		if (windField == null) {
			windField = new JTextField();
		}
		return windField;
	}

	/**
	 * This method initializes createButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCreateButton() {
		if (createButton == null) {
			createButton = new JButton();
			createButton.setText("CREATE");
		}
		return createButton;
	}

	/**
	 * This method initializes deleteButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getDeleteButton() {
		if (deleteButton == null) {
			deleteButton = new JButton();
			deleteButton.setText("DELETE");
		}
		return deleteButton;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
